<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>C++ Queues</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Queues</a>
  </div>

  <div class="name-format">
    back
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;queue&gt;
  <a href="../containers.html">TYPE</a>&amp; back();
  const <a href="../containers.html">TYPE</a>&amp; back() const;
</pre>

  <p>The back() function returns a reference to the last element in the
  queue.</p>

  <p>For example:</p>
  <pre class="example-code">
 queue&lt;int&gt; q;
 for( int i = 0; i &lt; 5; i++ ) {
   q.push(i);
 }
 cout &lt;&lt; &quot;The first element is &quot; &lt;&lt; q.front()
      &lt;&lt; &quot; and the last element is &quot; &lt;&lt; q.back() &lt;&lt; endl;           
</pre>

  <p>This code produces the following output:</p>
  <pre class="example-code">
 The first element is 0 and the last element is 4               
</pre>

  <p>The back() function runs in <a href="../complexity.html">constant
  time</a>.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="front.html">front</a><br>
    (C++ Lists) <a href="../cpplist/pop_back.html">pop_back</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    empty
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;queue&gt;
  bool empty() const;
</pre>

  <p>The empty() function returns true if the queue has no elements,
  false otherwise.</p>

  <p>For example, the following code uses empty() as the stopping
  condition on a <a href= "../keywords/while.html">while</a> loop to
  clear a queue while displaying its contents:</p>

  <pre class="example-code">
 queue&lt;int&gt; q;
 for( int i = 0; i &lt; 5; i++ ) {
   q.push(i);
 }
 while( !q.empty() ) {
   cout &lt;&lt; q.front() &lt;&lt; endl;
   q.pop();
 }              
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="size.html">size</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    front
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;queue&gt;
  <a href="../containers.html">TYPE</a>&amp; front();
  const <a href="../containers.html">TYPE</a>&amp; front() const;
</pre>

  <p>The front() function returns a reference to the first element of
  the queue, and runs in <a href="../complexity.html">constant
  time</a>.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="back.html">back</a><br>
    (C++ Lists) <a href="../cpplist/pop_front.html">pop_front</a><br>
    (C++ Lists) <a href="../cpplist/push_front.html">push_front</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    pop
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;queue&gt;
  void pop();
</pre>

  <p>The function pop() removes the first element of the queue and
  discards it.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="push.html">push</a><br>
    (C++ Priority Queues) <a href=
    "../cpppriority_queue/top.html">top</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    push
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;queue&gt;
  void push( const <a href="../containers.html">TYPE</a>&amp; val );
</pre>

  <p>The function push() adds <em>val</em> to the end of the current
  queue.</p>

  <p>For example, the following code uses the push() function to add
  ten integers to the end of a queue:</p>
  <pre class="example-code">
   queue&lt;int&gt; q;
   for( int i=0; i &lt; 10; i++ ) {
     q.push(i);
   }
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="pop.html">pop</a>
  </div>

  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    Queue constructor
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;queue&gt;
  queue();
  queue( const Container&amp; con );
</pre>

  <p>Queues have a default constructor as well as a copy constructor
  that will create a new queue out of the container <em>con</em>.
  </p>

  <p>For example, the following code creates a queue of strings,
  populates it with input from the user, and then displays it back to
  the user:</p>

  <pre class="example-code">
  queue&lt;string&gt; waiting_line;
  while( waiting_line.size() &lt; 5 ) {
    cout &lt;&lt; "Welcome to the line, please enter your name: ";
    string s;
    getline( cin, s );
    waiting_line.push(s);
  }

  while( !waiting_line.empty() ) {
    cout &lt;&lt; "Now serving: " &lt;&lt; waiting_line.front() &lt;&lt; endl;
    waiting_line.pop();
  }
</pre>

  <p>When run, the above code might produce this output:</p>

  <pre class="example-code">
  Welcome to the line, please enter your name: Nate
  Welcome to the line, please enter your name: lizzy 
  Welcome to the line, please enter your name: Robert B. Parker
  Welcome to the line, please enter your name: ralph
  Welcome to the line, please enter your name: Matthew
  Now serving: Nate
  Now serving: lizzy
  Now serving: Robert B. Parker
  Now serving: ralph
  Now serving: Matthew
</pre>

  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>

  <div class="name-format">
    size
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;queue&gt;
  <strong>size_type</strong> size() const;
</pre>

  <p>The size() function returns the number of elements in the current
  queue.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="empty.html">empty</a><br>
    (C++ Strings) <a href="../cppstring/capacity.html">capacity</a><br>
    (C++ Strings) <a href="../cppstring/length.html">length</a><br>
    (C++ Multimaps) <a href=
    "../cppmultimap/max_size.html">max_size</a><br>
    (C++ Strings) <a href="../cppstring/resize.html">resize</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
<hr>
</body></html>
